/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hyjiacan.deadqq.client.window;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import org.hyjiacan.deadqq.client.i18n.I18N;
import org.hyjiacan.deadqq.client.window.event.Regist;
import org.hyjiacan.deadqq.common.entity.User;
import org.hyjiacan.deadqq.common.util.WindowUtil;


/**
 * 注册新用户的窗口
 * 
 * @author hyjiacan
 */
public class RegistWindow extends JFrame implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8927965639773177862L;
	private LoginWindow lw;

	/**
	 * Creates new form RegistWindow
	 */
	public RegistWindow(LoginWindow lw) {
		this.lw = lw;
	}

	@Override
	public void run() {
		initComponents();
		WindowUtil.center(this);
		this.setVisible(true);
		WindowManager.put(this.getClass().getName(), this);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		gender = new ButtonGroup();
		nicknamelb = new JLabel();
		pswdlb = new JLabel();
		cfmpswdlb = new JLabel();
		genderlb = new JLabel();
		namelb = new JLabel();
		validCodelb = new JLabel();
		nickname = new JTextField();
		name = new JTextField();
		male = new JRadioButton();
		female = new JRadioButton();
		pswd = new JPasswordField();
		confirmPswd = new JPasswordField();
		validCode = new JTextField();
		validCodeImg = new JLabel();
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		birthday = new JLabel();
		month = new JComboBox();
		monthlb = new JLabel();
		day = new JComboBox();
		daylb = new JLabel();
		regist = new JButton();
		cancel = new JButton();

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle(I18N.getString("REGNEWUSER")); // NOI18N
		setResizable(false);

		nicknamelb.setText(I18N.getString("NICKNAME")); // NOI18N

		pswdlb.setText(I18N.getString("PASSWORD")); // NOI18N

		cfmpswdlb.setText(I18N.getString("CONFIRM PASSWORD")); // NOI18N

		genderlb.setText(I18N.getString("GENDER")); // NOI18N

		namelb.setText(I18N.getString("REALNAME")); // NOI18N

		validCodelb.setText(I18N.getString("VERIFICATION")); // NOI18N

		gender.add(male);
		male.setSelected(true);
		male.setText(I18N.getString("MALE")); // NOI18N

		gender.add(female);
		female.setText(I18N.getString("FEMALE")); // NOI18N

		jLabel1.setForeground(new java.awt.Color(255, 0, 0));
		jLabel1.setText(I18N.getString("*")); // NOI18N

		jLabel2.setForeground(new java.awt.Color(255, 0, 0));
		jLabel2.setText(I18N.getString("*")); // NOI18N

		jLabel3.setForeground(new java.awt.Color(255, 0, 0));
		jLabel3.setText(I18N.getString("*")); // NOI18N

		birthday.setText(I18N.getString("BIRTHDAY")); // NOI18N

		month.setModel(new DefaultComboBoxModel(new String[] { "Item 1",
				"Item 2", "Item 3", "Item 4" }));

		monthlb.setText(I18N.getString("MONTH")); // NOI18N

		day.setModel(new DefaultComboBoxModel(new String[] { "Item 1",
				"Item 2", "Item 3", "Item 4" }));

		daylb.setText(I18N.getString("DAY")); // NOI18N

		regist.setText(I18N.getString("REGIST")); // NOI18N
		regist.addActionListener(new Regist());

		cancel.setText(I18N.getString("CANCEL")); // NOI18N
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelActionPerformed(evt);
			}
		});

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(5, 5, 5)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addComponent(
														birthday,
														GroupLayout.Alignment.TRAILING)
												.addComponent(
														pswdlb,
														GroupLayout.Alignment.TRAILING)
												.addComponent(
														nicknamelb,
														GroupLayout.Alignment.TRAILING)
												.addComponent(
														validCodelb,
														GroupLayout.Alignment.TRAILING)
												.addComponent(
														namelb,
														GroupLayout.Alignment.TRAILING)
												.addComponent(
														genderlb,
														GroupLayout.Alignment.TRAILING)
												.addComponent(
														cfmpswdlb,
														GroupLayout.Alignment.TRAILING))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addComponent(pswd)
												.addComponent(nickname)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(0,
																		0,
																		Short.MAX_VALUE)
																.addComponent(
																		cancel)
																.addPreferredGap(
																		LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		regist))
												.addComponent(confirmPswd)
												.addComponent(name)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										male)
																								.addGap(30,
																										30,
																										30)
																								.addComponent(
																										female))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										month,
																										GroupLayout.PREFERRED_SIZE,
																										GroupLayout.DEFAULT_SIZE,
																										GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										LayoutStyle.ComponentPlacement.UNRELATED)
																								.addComponent(
																										monthlb,
																										GroupLayout.PREFERRED_SIZE,
																										39,
																										GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										LayoutStyle.ComponentPlacement.UNRELATED)
																								.addComponent(
																										day,
																										GroupLayout.PREFERRED_SIZE,
																										GroupLayout.DEFAULT_SIZE,
																										GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										LayoutStyle.ComponentPlacement.UNRELATED)
																								.addComponent(
																										daylb,
																										GroupLayout.PREFERRED_SIZE,
																										29,
																										GroupLayout.PREFERRED_SIZE))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										validCode,
																										GroupLayout.PREFERRED_SIZE,
																										78,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(18,
																										18,
																										18)
																								.addComponent(
																										validCodeImg,
																										GroupLayout.PREFERRED_SIZE,
																										68,
																										GroupLayout.PREFERRED_SIZE)))
																.addGap(0,
																		2,
																		Short.MAX_VALUE)))
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel3,
																		GroupLayout.PREFERRED_SIZE,
																		20,
																		GroupLayout.PREFERRED_SIZE)
																.addContainerGap(
																		20,
																		Short.MAX_VALUE))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel1,
																		GroupLayout.PREFERRED_SIZE,
																		17,
																		GroupLayout.PREFERRED_SIZE)
																.addGap(0,
																		0,
																		Short.MAX_VALUE))
												.addComponent(
														jLabel2,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addGap(16, 16, 16)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(nicknamelb)
										.addComponent(nickname,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel1))
						.addGap(18, 18, 18)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(pswdlb)
										.addComponent(pswd,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel2))
						.addGap(20, 20, 20)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(cfmpswdlb)
										.addComponent(confirmPswd,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel3))
						.addGap(18, 18, 18)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(namelb)
										.addComponent(name,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(18, 18, 18)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(genderlb)
										.addComponent(male)
										.addComponent(female))
						.addGap(19, 19, 19)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(birthday)
										.addComponent(month,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(monthlb)
										.addComponent(day,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(daylb))
						.addPreferredGap(
								LayoutStyle.ComponentPlacement.RELATED, 27,
								Short.MAX_VALUE)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(validCodelb)
										.addComponent(validCode,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(validCodeImg))
						.addGap(38, 38, 38)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(regist)
										.addComponent(cancel))
						.addGap(25, 25, 25)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void cancelActionPerformed(ActionEvent evt) {// GEN-FIRST:event_cancelActionPerformed
		lw.setVisible(true);
		this.dispose();
	}// GEN-LAST:event_cancelActionPerformed

	public User getUser() {
		User u = new User();
		String nn = nickname.getText().trim();
		String pd = String.valueOf(pswd.getPassword());
		String cpd = String.valueOf(confirmPswd.getPassword());
		if (nn.isEmpty() || pd.isEmpty() || cpd.isEmpty() || !pd.equals(cpd)) {
			System.out.println("注册信息输入不正确");
			return null;
		} else {
			u.setNickname(nn);
			u.setPassword(pd);
			return u;
		}
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JLabel birthday;
	private JButton cancel;
	private JLabel cfmpswdlb;
	private JPasswordField confirmPswd;
	@SuppressWarnings("rawtypes")
	private JComboBox day;
	private JLabel daylb;
	private JRadioButton female;
	private ButtonGroup gender;
	private JLabel genderlb;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JRadioButton male;
	@SuppressWarnings("rawtypes")
	private JComboBox month;
	private JLabel monthlb;
	private JTextField name;
	private JLabel namelb;
	private JTextField nickname;
	private JLabel nicknamelb;
	private JPasswordField pswd;
	private JLabel pswdlb;
	private JButton regist;
	private JTextField validCode;
	private JLabel validCodeImg;
	private JLabel validCodelb;
	// End of variables declaration//GEN-END:variables

}
